package com.csw.android.player.ui.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import androidx.annotation.StringRes
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.csw.android.dev_utils.ex.ViewEx.pxInt
import com.csw.android.dev_utils.utils.TableMarginDecoration
import com.csw.android.player.R
import com.csw.android.player.base.OnControllerListener
import com.csw.android.player.base.VideoRatio
import com.csw.android.player.databinding.PlayerDialogSurfaceSettingBinding
import com.google.android.material.bottomsheet.BottomSheetDialog

class SurfaceSettingDialog(context: Context) :
    BottomSheetDialog(context, R.style.Player_NormalBottomSheetDialogStyle) {

    var onControllerListener: OnControllerListener? = null

    private val dataBinding: PlayerDialogSurfaceSettingBinding = DataBindingUtil.inflate(
        LayoutInflater.from(context),
        R.layout.player_dialog_surface_setting,
        null,
        false
    )

    init {
        dataBinding.run {
            setContentView(root)
            recyclerView.run {
                layoutManager = LinearLayoutManager(context)
                adapter = FunctionAdapter()
            }
            root.post {
                behavior.run {
                    peekHeight = root.height
                }
            }
        }
    }

    private class FunctionItem(
        @StringRes val nameRes: Int,
        val adapter: RecyclerView.Adapter<BaseViewHolder>
    )

    private inner class FunctionAdapter : BaseQuickAdapter<FunctionItem, BaseViewHolder>(
        R.layout.player_item_surface_setting_function
    ) {

        init {
            addData(
                FunctionItem(
                    R.string.player_surface_setting_dialog_fill_mode,
                    ScaleTypeAdapter()
                )
            )
            addData(
                FunctionItem(
                    R.string.player_surface_setting_dialog_video_ratio,
                    VideoRatioAdapter()
                )
            )
            addData(
                FunctionItem(
                    R.string.player_surface_setting_dialog_video_mirror,
                    MirrorAdapter()
                )
            )
        }

        override fun convert(holder: BaseViewHolder, item: FunctionItem) {
            holder.setText(R.id.name, item.nameRes)
            holder.getView<RecyclerView>(R.id.recyclerView).run {
                if (layoutManager == null) {
                    layoutManager =
                        LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                    addItemDecoration(object : TableMarginDecoration() {
                        private val dp5 = pxInt(5f)
                        private val dp10 = pxInt(10f)
                        override fun getItemOffset(
                            view: View,
                            parent: RecyclerView,
                            state: RecyclerView.State,
                            column: Int,
                            columnCount: Int,
                            row: Int,
                            rowCount: Int,
                            direction: Direction
                        ): Int {
                            return when {
                                direction == Direction.LEFT && column == 0 -> dp10
                                direction == Direction.RIGHT && column == columnCount - 1 -> dp10
                                else -> dp5
                            }
                        }
                    })
                }
                adapter = item.adapter
            }
        }
    }

    //region scaleType------------------------------------------------------------------------------
    private var scaleType = ImageView.ScaleType.FIT_CENTER

    private class ScaleTypeItem(@StringRes val nameRes: Int, val value: ImageView.ScaleType)
    private inner class ScaleTypeAdapter : BaseQuickAdapter<ScaleTypeItem, BaseViewHolder>(
        R.layout.player_item_normal_setting_text
    ) {

        init {
            addData(
                ScaleTypeItem(
                    R.string.player_surface_setting_dialog_fill_mode_origin,
                    ImageView.ScaleType.CENTER_INSIDE
                )
            )
            addData(
                ScaleTypeItem(
                    R.string.player_surface_setting_dialog_fill_mode_fit_center,
                    ImageView.ScaleType.FIT_CENTER
                )
            )
            addData(
                ScaleTypeItem(
                    R.string.player_surface_setting_dialog_fill_mode_fit_xy,
                    ImageView.ScaleType.FIT_XY
                )
            )
            addData(
                ScaleTypeItem(
                    R.string.player_surface_setting_dialog_fill_mode_center_crop,
                    ImageView.ScaleType.CENTER_CROP
                )
            )
            setOnItemClickListener { _, view, position ->
                getItem(position).run {
                    if (scaleType != value) {
                        scaleType = value
                        notifyDataSetChanged()
                        onControllerListener?.onScaleTypeChange(scaleType)
                    }
                }
            }
        }

        override fun convert(holder: BaseViewHolder, item: ScaleTypeItem) {
            holder.setText(R.id.name, item.nameRes)
            holder.itemView.isSelected = scaleType == item.value
        }
    }
    //endregion


    //region videoRatio------------------------------------------------------------------------------
    private var videoRatio = VideoRatio.ORIGIN

    private class VideoRatioItem(@StringRes val nameRes: Int, val value: VideoRatio)
    private inner class VideoRatioAdapter : BaseQuickAdapter<VideoRatioItem, BaseViewHolder>(
        R.layout.player_item_normal_setting_text
    ) {

        init {
            addData(
                VideoRatioItem(
                    R.string.player_surface_setting_dialog_video_ratio_origin,
                    VideoRatio.ORIGIN
                )
            )
            addData(
                VideoRatioItem(
                    R.string.player_surface_setting_dialog_video_ratio_4_3,
                    VideoRatio.W4_H3
                )
            )
            addData(
                VideoRatioItem(
                    R.string.player_surface_setting_dialog_video_ratio_16_9,
                    VideoRatio.W16_H9
                )
            )
            addData(
                VideoRatioItem(
                    R.string.player_surface_setting_dialog_video_ratio_21_9,
                    VideoRatio.W21_H9
                )
            )
            setOnItemClickListener { _, view, position ->
                getItem(position).run {
                    if (videoRatio != value) {
                        videoRatio = value
                        notifyDataSetChanged()
                        onControllerListener?.onVideoRatioChange(videoRatio)
                    }
                }
            }
        }

        override fun convert(holder: BaseViewHolder, item: VideoRatioItem) {
            holder.setText(R.id.name, item.nameRes)
            holder.itemView.isSelected = videoRatio == item.value
        }
    }
    //endregion

    //region mirror------------------------------------------------------------------------------
    private var mirror = false

    private class MirrorItem(@StringRes val nameRes: Int, val value: Boolean)
    private inner class MirrorAdapter : BaseQuickAdapter<MirrorItem, BaseViewHolder>(
        R.layout.player_item_normal_setting_text
    ) {

        init {
            addData(
                MirrorItem(
                    R.string.player_surface_setting_dialog_video_mirror_disable,
                    false
                )
            )
            addData(
                MirrorItem(
                    R.string.player_surface_setting_dialog_video_mirror_enable,
                    true
                )
            )
            setOnItemClickListener { _, view, position ->
                getItem(position).run {
                    if (mirror != value) {
                        mirror = value
                        notifyDataSetChanged()
                        onControllerListener?.onMirrorChange(mirror)
                    }
                }
            }
        }

        override fun convert(holder: BaseViewHolder, item: MirrorItem) {
            holder.setText(R.id.name, item.nameRes)
            holder.itemView.isSelected = mirror == item.value
        }
    }
    //endregion
}